#pragma once
#include "../comm/util.hpp"
#include "../comm/log.hpp"
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <fstream>
#include <cstdlib>
#include <cassert>

namespace ns_model
{
    using namespace std;
    using namespace ns_log;
    using namespace ns_util;
    struct Question
    {
        std::string number; // 题目编号，唯一
        std::string title;  // 题目的标题
        std::string star;   // 难度: 简单 中等 困难

        int cpu_limit;      // 题目的时间要求(S)
        int mem_limit;      // 题目的空间要去(KB)
        std::string desc;   // 题目的描述
        std::string header; // 题目预设给用户在线编辑器的代码
        std::string tail;   // 题目的测试用例，需要和header拼接，形成完整代码
    };
    const std::string questins_list = "./questions/questions.list";
    const std::string questins_path = "./questions/";

    class Model
    {
    private:
        unordered_map<string, struct Question> questions;
        

    public:
        Model()
        {
            LoadQuestionList(questins_list);
        }
        ~Model()
        {
        }
        bool LoadQuestionList(const string &question_list)
        {
            // 加载配置文件: questions/questions.list + 题目编号文件
            ifstream in(questins_list);

            if (!in.is_open())
            {
                FATAL_Log("题目列表获取失败");
                return false;
            }
            string line;
            while (getline(in, line))
            {
                vector<string> tokens;
                string sep = " ";
                StringUtil::split(line, sep, tokens);
                // 1 判断回文数 简单 1 30000
                if (tokens.size() != 5)
                {
                    WARING_Log("加载题目格式出错");
                    continue;
                }
                Question q;
                q.number = tokens[0];
                q.title = tokens[1];
                q.star = tokens[2];
                q.cpu_limit = atoi(tokens[3].c_str());
                q.mem_limit = atoi(tokens[4].c_str());

                string path = questins_path;
                path += q.number;
                path += "/";
                FileUtil::ReadFile(path+"desc.txt", &(q.desc), true);
                FileUtil::ReadFile(path+"header.cpp", &(q.header), true);
                FileUtil::ReadFile(path+"tail.cpp", &(q.tail), true);
                questions.insert({q.number, q});
            }
            
            return true;
        }
        bool GetAllQuestions(vector<Question> *out)
        {
            if(questions.size()==0)
            {
                ERR_Log("获取题库出错");
                return false;
            }
            for(auto it:questions)
            {
                out->push_back(it.second);
                
            }

            return true;
        }
        bool GetOneQuestion(const std::string &number, Question *q)
        {
            const auto it=questions.find(number);
            if(it==questions.end())
            {
                ERR_Log("获取题目：%s出错",number.c_str());
                return false;
            }
            *q=it->second;
            return true;
        }
    };

}